package com.chat.ui.library;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chat.MyApplication;
import com.chat.R;
import com.chat.entity.DataPackage;
import com.chat.entity.Room;
import com.chat.entity.ServerAction;
import com.chat.entity.UserJoinRoomForm;
import com.chat.entity.event.ChannelEvent;
import com.chat.net.SocketClient;
import com.chat.net.socket.Channel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审核加入聊天室申请
 */
public class LibraryFragment extends Fragment {
    // 自动完成文本框
    RecyclerView recyclerView;
    private List<HashMap<String,Object>> mData=new ArrayList<HashMap<String,Object>>();
    private LibraryFragment.MyRecycleViewAdapter adapter;
    private List<Room> rooms = null;

    final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1)
                getData();
            else if(msg.what == 200)
                adapter.notifyDataSetChanged();
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_library, container, false);
        //获取管理的聊天室
        SocketClient.getSocketClient().addChannelEvent(new ChannelEvent() {
            @Override
            public void onChannelEvent(Channel ctx, DataPackage source) {
                if(source.getCode() == 200){
                    rooms = (List<Room>) source.getDate();
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            }
        },DataPackage.request(null,ServerAction.GET_ROOMS_BY_USER));
        recyclerView = view.findViewById(R.id.apply_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//垂直线性布局
        adapter=new LibraryFragment.MyRecycleViewAdapter();
        recyclerView.setAdapter(adapter);


        return view;
    }


    public void getData(){



        //获取加入聊天室请求
        if(rooms!=null){
            for(Room room:rooms){
                Map<String,Integer> params = new HashMap<>();
                params.put("roomId",room.getRoomId());
                DataPackage dataPackage = DataPackage.request(params, ServerAction.GET_ROOM_FORMS);
                SocketClient.getSocketClient().addChannelEvent(new ChannelEvent() {//添加处理事件
                    @Override
                    public void onChannelEvent(Channel ctx, DataPackage source) {
                        List<UserJoinRoomForm> list = (List<UserJoinRoomForm>) source.getDate();
                        for(UserJoinRoomForm userJoinRoomForm:list){
                            HashMap<String,Object> item = new HashMap<>();
                            item.put("id",userJoinRoomForm.getId());
                            item.put("name",userJoinRoomForm.getUserId());
                            item.put("roomId",userJoinRoomForm.getRoomId());
                            item.put("picture",R.drawable.dog);
                            mData.add(item);
                        }
                        Message message = Message.obtain();
                        message.what = source.getCode();
                        handler.sendMessage(message);
                    }
                },dataPackage);
            }
        }



    }




    class  MyRecycleViewAdapter extends RecyclerView.Adapter<LibraryFragment.MyRecycleViewAdapter.ViewHolder>
    {


        public  class ViewHolder extends RecyclerView.ViewHolder {
            public Button btn_yes,btn_no;
            public TextView name;
            public ImageView head;

            public ViewHolder(View convertView) {
                super(convertView);
                head = (ImageView)convertView.findViewById(R.id.item_apply_head);
                name = (TextView)convertView.findViewById(R.id.item_apply_name);
                btn_no = (Button) convertView.findViewById(R.id.item_apply_btn_no);
                btn_yes = (Button) convertView.findViewById(R.id.item_apply_btn_yes);
            }
        }

        @NonNull
        @Override
        public LibraryFragment.MyRecycleViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v= LayoutInflater.from(getActivity()).inflate(R.layout.item_apply,parent, false);
            return new LibraryFragment.MyRecycleViewAdapter.ViewHolder(v);
        }



        @Override
        public void onBindViewHolder(@NonNull LibraryFragment.MyRecycleViewAdapter.ViewHolder holder, final int position) {
            //picture
            Glide.with(getActivity()).load(new MyApplication().imagebaseurl+mData.get(position).get("picture").toString()).placeholder(R.drawable.dog).into(holder.head);
            holder.name.setText("用户:"+String.valueOf(mData.get(position).get("name"))+"申请加入\n聊天室:"+String.valueOf((mData.get(position).get("roomId"))));
            final Map<String,Integer> params = new HashMap<>();
            params.put("formId", (Integer) mData.get(position).get("id"));


            final DataPackage dataPackage = DataPackage.request(ServerAction.JUDGED_ROOM_FORM);
            holder.btn_no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    params.put("result",0);
                    dataPackage.setDate(params);
                    mData.remove(position);
                    SocketClient.getSocketClient().addChannelEvent(new ChannelEvent() {
                        @Override
                        public synchronized void onChannelEvent(Channel ctx, DataPackage source) {
                            super.onChannelEvent(ctx, source);
                        }
                    }, dataPackage);
                    Message message = Message.obtain();
                    message.what = 200;
                    handler.sendMessage(message);
                }
            });

            holder.btn_yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    params.put("result",1);
                    dataPackage.setDate(params);
                    SocketClient.getSocketClient().addChannelEvent(new ChannelEvent() {
                        @Override
                        public synchronized void onChannelEvent(Channel ctx, DataPackage source) {
                            super.onChannelEvent(ctx, source);
                        }
                    },dataPackage);
                    mData.remove(position);
                    Message message = Message.obtain();
                    message.what = 200;
                    handler.sendMessage(message);
                }
            });


        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }


}
